<template>
  <div>
    <list
      ref="list"
      :disabled="!$permission.canRead()"
    >
      <template #toolbar-left>
        <el-button
          @click="syncCustomersToIcp"
          type="primary"
          :loading="syncIcpLoading"
          :disabled="!$permission.canWrite()"
        >同步到旧ICP</el-button>
        <el-button
          @click="syncCustomersToEs"
          type="primary"
          :loading="syncEsLoading"
          :disabled="!$permission.canWrite()"
        >同步到ES</el-button>
        <el-button
          @click="resetNameUniqueValue"
          type="primary"
          :loading="resetLoading"
          :disabled="!$permission.canWrite()"
        >重置客户名称唯一值</el-button>
        <el-button
          @click="openExamineStatusExceptionListDialog"
          type="primary"
          :disabled="!$permission.canRead()"
        >客户审批状态异常列表</el-button>
        <el-button
          @click="decodeCustomerLocalizationText"
          type="primary"
          :disabled="!$permission.canRead()"
          :loading="decodeLoading"
        >反转义多语言字段</el-button>
      </template>
      <!-- <template #column>
        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          width="80"
          v-if="$permission.canWrite()"
        >
          <template slot-scope="{row}">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="edit(row)"
              :disabled="row.id<0"
            />
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="del(row)"
              :disabled="row.id<0"
            />
          </template>
        </el-table-column>
      </template> -->
    </list>
    <examine-status-exception-list-dialog ref="examineStatusExceptionListDialog" />
  </div>
</template>

<script>
import list from "@/components/crm/customer/list";
import examineStatusExceptionListDialog from "@/components/crm/customer/examineStatusExceptionListDialog";
import curdMixins from "@/mixins/curd";
import api from "@/api/crm/customer";
export default {
  components: {
    list,
    examineStatusExceptionListDialog,
  },
  mixins: [curdMixins],
  data() {
    return {
      syncEsLoading: false,
      syncIcpLoading: false,
      resetLoading: false,
      decodeLoading: false
    };
  },
  created() { },
  methods: {
    syncCustomersToEs() {
      var rows = this.$refs.list.getCheckedRows();
      let message = "确定要将选中的客户同步到ES吗？";
      let postData;
      if (rows && rows.length > 0) {
        postData = { ids: rows.map((item) => item.id) };
      } else {
        message = "确定要将客户列表查询结果同步到ES吗？"
        postData = this.$refs.list.$refs.list.tempQueryParams;
      }
      this.$confirm(message, "提示", {
        type: "warning",
      })
        .then(() => {
          this.syncEsLoading = true;
          console.log(postData);
          api.syncCustomersToEs(postData).then((res) => {
            this.syncEsLoading = false;
          });
        })
        .catch(() => { });
    },
    syncCustomersToIcp() {
      var rows = this.$refs.list.getCheckedRows();
      let message = "确定要将选中的客户同步到旧ICP吗？";
      let postData;
      if (rows && rows.length > 0) {
        postData = { ids: rows.map((item) => item.id) };
      } else {
        message = "确定要将客户列表查询结果同步到旧ICP吗？"
        postData = this.$refs.list.$refs.list.tempQueryParams;
      }
      this.$confirm(message, "提示", {
        type: "warning",
      })
        .then(() => {
          this.syncIcpLoading = true;
          console.log(postData);
          api.syncCustomersToIcp(postData).then((res) => {
            this.syncIcpLoading = false;
          });
        })
        .catch(() => { });
    },
    resetNameUniqueValue() {
      var rows = this.$refs.list.getCheckedRows();
      let message = "确定要将选中的客户重置名称唯一值吗？";
      let postData;
      if (rows && rows.length > 0) {
        postData = { ids: rows.map((item) => item.id) };
      } else {
        message = "确定要将把客户列表查询结果进行重置名称唯一值吗？"
        postData = this.$refs.list.$refs.list.tempQueryParams;
      }
      this.$confirm(message, "提示", {
        type: "warning",
      })
        .then(() => {
          this.resetLoading = true;
          console.log(postData)
          api.resetNameUniqueValue(postData).then((res) => {
            this.resetLoading = false;
          });
        })
        .catch((e) => {
          console.log(e);
        });
    },
    openExamineStatusExceptionListDialog() {
      this.$refs.examineStatusExceptionListDialog.open();
    },
    decodeCustomerLocalizationText() {
      var rows = this.$refs.list.getCheckedRowsWithMessage();
      if (rows.length == 0) {
        return;
      }
      let message = "确定要将选中的客户反转义多语言字段吗？";
      let postData;
      postData = { ids: rows.map((item) => item.id) };
      this.$confirm(message, "提示", {
        type: "warning",
      })
        .then(() => {
          this.decodeLoading = true;
          console.log(postData)
          api.decodeCustomerLocalizationText(postData).then((res) => {
            this.decodeLoading = false;
          });
        })
        .catch((e) => {
          console.log(e);
        });
    },
  },
};
</script>